package com.fanshuai.jmx.agent;

import com.fanshuai.jmx.mbean.Hello;
import com.fanshuai.jmx.mbean.Jack;
import com.fanshuai.jmx.notification.HelloListener;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;

public class JMXAgentNotification {
    public static void init() throws Exception {
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();

        ObjectName objectName = new ObjectName("custom:name=hello");
        ObjectName jackName = new ObjectName("custom:name=jack");

        Hello hello = new Hello();
        hello.setAge(18);
        hello.setName("林芳伊");

        Jack jack = new Jack();
        jack.addNotificationListener(new HelloListener(), null, hello); //添加listener

        mBeanServer.registerMBean(hello, objectName);
        mBeanServer.registerMBean(jack, jackName);
    }
}
